'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const now = new Date();
    
    // 先删除已存在的配置（强制替换）
    await queryInterface.bulkDelete('table_configs', {
      code: 'roles'
    }, {});
    
    // 添加角色表配置
    await queryInterface.bulkInsert('table_configs', [{
      id: Sequelize.literal('UUID()'),
      code: 'roles',
      name: '角色管理',
      description: '系统角色信息管理',
      table_name: 'roles',
      api_path: '/roles',

      fields: JSON.stringify([
        {
          key: 'id',
          label: 'ID',
          type: 'text',
          table: {
            hidden: true,
            width: 200,
            align: 'left',
            fixed: false,
            sortable: false,
            ellipsis: false
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'name',
          label: '角色名称',
          type: 'text',
          table: {
            width: 150,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false
          },
          search: {
            searchable: true,
            searchType: 'like',
            quickSearch: true
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: true,
            rules: [
              { required: true, message: '请输入角色名称' }
            ],
            placeholder: '请输入角色名称'
          }
        },
        {
          key: 'description',
          label: '角色描述',
          type: 'textarea',
          table: {
            width: 250,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: true
          },
          search: {
            searchable: false
          },
          form: {
            creatable: true,
            editable: true,
            updatable: true,
            required: false,
            placeholder: '请输入角色描述'
          }
        },
        {
          key: 'isSystem',
          label: '系统角色',
          type: 'select',
          table: {
            width: 120,
            align: 'center',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false,
            render: 'tag',
            tagColorMap: {
              true: 'blue',
              false: 'green'
            },
            tagLabelMap: {
              true: '系统角色',
              false: '自定义角色'
            }
          },
          search: {
            searchable: true,
            searchType: 'equal'
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'tenantId',
          label: '所属租户',
          type: 'text',
          table: {
            width: 120,
            align: 'center',
            fixed: false,
            hidden: false,
            sortable: false,
            ellipsis: false,
            render: 'tag',
            tagColorMap: {
              null: 'purple',
              '': 'purple'
            },
            tagLabelMap: {
              null: '系统共享',
              '': '系统共享'
            }
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'createdAt',
          label: '创建时间',
          type: 'datetime',
          table: {
            width: 180,
            align: 'left',
            fixed: false,
            hidden: false,
            sortable: true,
            ellipsis: false,
            format: 'YYYY-MM-DD HH:mm:ss'
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        },
        {
          key: 'updatedAt',
          label: '更新时间',
          type: 'datetime',
          table: {
            width: 180,
            align: 'left',
            fixed: false,
            hidden: true,
            sortable: true,
            ellipsis: false,
            format: 'YYYY-MM-DD HH:mm:ss'
          },
          search: {
            searchable: false
          },
          form: {
            creatable: false,
            editable: false,
            updatable: false
          }
        }
      ]),

      actions: JSON.stringify({
        create: true,
        update: true,
        delete: true,
        batchDelete: true,
        export: true,
        import: false
      }),

      table_props: JSON.stringify({
        size: 'middle',
        rowKey: 'id',
        bordered: false,
        pagination: {
          pageSize: 10,
          showSizeChanger: true,
          showQuickJumper: true
        },
        rowSelection: true,
        scroll: { x: 1200 }
      }),

      permissions: JSON.stringify({
        view: 'role:read',
        create: 'role:create',
        update: 'role:update',
        delete: 'role:delete'
      }),

      status: true,
      created_by: null,
      updated_by: null,
      created_at: now,
      updated_at: now
    }], {});
  },

  down: async (queryInterface, Sequelize) => {
    // 删除角色表配置
    await queryInterface.bulkDelete('table_configs', {
      code: 'roles'
    }, {});
  }
};

